package cn.edu.zut.internship.service.impl.company;

import cn.edu.zut.internship.bean.sys.Result;
import cn.edu.zut.internship.bean.sys.SysUser;
import cn.edu.zut.internship.iservice.company.CompanySV;
import cn.edu.zut.internship.iservice.notify.NotifySV;
import cn.edu.zut.internship.mapper.company.CompanyMapper;
import cn.edu.zut.internship.utils.PrimaryKeyUtil;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class CompanySVImpl implements CompanySV {
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    PrimaryKeyUtil primaryKeyUtil;
    @Autowired
    NotifySV notifySV;
    @Override
    public Result getCompanyById(Map params) {
        Result result = new Result();
        Map company = companyMapper.getCompany(params);
        //图片转数组列表
        List<String> pictureUrlList = new ArrayList<>();
        for(int i = 1;i<=5;i++){
            String pictureUrl = "pictureUrl"+i;
            if(company.get(pictureUrl)!=null&&!"".equals((String)company.get(pictureUrl))){
                pictureUrlList.add((String)company.get(pictureUrl));
            }
        }
        //保留一位小数
        Double score = new Double((float) company.get("score"));
        NumberFormat nf=new DecimalFormat( "0.0");
        score = Double.parseDouble(nf.format(score));
        company.put("score",score);
        result.setBean(company);
        result.getBean().put("pictureUrlList",pictureUrlList);
        return result;
    }

    @Override
    public Result getMyCompany(String userAccount) {
        Result result = new Result();
        Map myCompany = companyMapper.getMyCompany(userAccount);
        result.setBean(myCompany);
        return result;
    }

    @Override
    public Result selectCompany(Map params) {
        Result result = new Result();
        List<Map> list = companyMapper.getList(params);
        int listCount = companyMapper.getListCount(params);
        for(Map company:list){
            int companyType = (int) company.get("companyType");
            if(companyType == 0){
                company.put("companyTypeName","校内合作实训单位");
            }else if(companyType == 1){
                company.put("companyTypeName","校外合作实训单位");
            }else if(companyType == 2){
                company.put("companyTypeName","非合作实训单位");
            }
        }
        //处理评分
        for(Map map :list){
            //保留一位小数
            Double score = new Double((float) map.get("score"));
            NumberFormat nf=new DecimalFormat( "0.0");
            score = Double.parseDouble(nf.format(score));
            map.put("score",score);
        }
        result.setBeans(list);
        result.getBean().put("total",listCount);
        return result;
    }

    @Override
    public Result getVerifyCompanyList(Map params) {
        Result result = new Result();
        params.put("internshipStatus","0");
        result = selectCompany(params);
        return result;
    }

    @Override
    public Result passCompny(Map params) {
        Result result = new Result();
        SysUser userInfo = (SysUser) params.get("userInfo");
        Map company = companyMapper.getCompany(params);
        companyMapper.passCompny(params);
        //审核通过，给该单位上传人发送消息
        Map notify = new HashMap();
        notify.put("sendAccount",userInfo.getUserAccount());
        notify.put("sendType",0);
        notify.put("notifyType",1);
        notify.put("internshipId",params.get("internshipId"));
        notify.put("content","管理员"+userInfo.getUserName()+"已审核通过你上传的实训单位");
        notify.put("targetAccount",company.get("creatAccount"));
        notify.put("targetType",0);
        notify.put("creatAccount",params.get("teachAccount"));
        notifySV.addNotify(notify);
        return result;
    }

    @Override
    public Result choiceCompany(Map params) {
        String userAccount = (String) params.get("creatAccount");
        Result result = new Result();
        //查询当前是否存在绑定关系
        Map myCompany = companyMapper.getMyCompany(userAccount);
        //存在绑定关系
        if(CollectionUtils.isEmpty(myCompany)) {
            //不存在绑定关系
            Long siId = primaryKeyUtil.generatePrimaryKey("student_internship");
            params.put("siId", siId);
            params.put("studentAccount", params.get("creatAccount"));
            companyMapper.choiceCompany(params);
        }else{
            result.setCode(-9999);
            result.setMessage("已经绑定了实训单位，请解除绑定后再来绑定此实训单位哦！");
        }
        return result;
    }

    @Override
    public Result addCompany(Map params) {
        Result result = new Result();
        Long companyId = primaryKeyUtil.generatePrimaryKey("internship");
        params.put("internshipId",companyId);
        params.put("delFlag",0);
        params.put("internshipStatus",0);
        //处理图片链接格式
        List<String> imageUrls = (List) params.get("pictureUrlList");
        if(!CollectionUtils.isEmpty(imageUrls)){
            int i = 1;
            for(String url:imageUrls){
                String pictureUrl = "pictureUrl"+i;
                params.put(pictureUrl,url);
                i++;
            }
        }
        SysUser userInfo = (SysUser) params.get("userInfo");
        if(userInfo.getUserType() == 0||userInfo.getUserType() == 1){//教师管理员增加的实训单位无需审核
            params.put("internshipStatus",1);
        }
        companyMapper.addCompany(params);
        return result;
    }
    @Override
    public Result updateCompany(Map params) {
        Result result = new Result();
        companyMapper.updateCompany(params);
        return result;
    }

    @Override
    public Result editMyCompany(Map params) {
        Result result = new Result();
        int internshipStatus = (int) params.get("internshipStatus");
        if(internshipStatus==2){
            params.put("internshipStatus",0);
        }
        companyMapper.editMyCompany(params);
        return result;
    }

    @Override
    public Result delectCompany(Long companyId) {
        Result result = new Result();
        companyMapper.deleteCompany(companyId);
        return result;
    }

    @Override
    public Result addCompanyHistory(Map params) {
        Result result = new Result();
        List<Map> companyList = companyMapper.addCompanyHistory(params);
        for(Map map :companyList){
            int internshipStatus = (int) map.get("internshipStatus");
            if(internshipStatus == 0){
                map.put("internshipStatusName","待审核");
            }else if(internshipStatus == 1){
                map.put("internshipStatusName","审核通过");
            }else if(internshipStatus == 2){
                map.put("internshipStatusName","已驳回");
            }
            int internshipType = (int) map.get("internshipType");
            if(internshipType == 0){
                map.put("internshipTypeName","校内合作实训单位");
            }else if(internshipType == 1){
                map.put("internshipTypeName","校外合作实训单位");
            }else if(internshipType == 2){
                map.put("internshipTypeName","非合作实训单位");
            }
        }
        result.setBeans(companyList);
        int total = companyMapper.addCompanyHistoryCount(params);
        result.getBean().put("total",total);
        return result;
    }

    @Override
    public Result editCompany(Map params) {
        Result result = new Result();
        //处理图片链接格式
        List<String> imageUrls = (List) params.get("imageUrls");
        if(!CollectionUtils.isEmpty(imageUrls)){
            int i = 1;
            for(String url:imageUrls){
                String pictureUrl = "pictureUrl"+i;
                params.put(pictureUrl,url);
                i++;
            }
        }
        params.put("internshipStatus",0);
        params.put("reject","");
        companyMapper.updateCompany(params);
        return result;
    }

    @Override
    public Result rejectCompany(Map params) {
        Result result = new Result();
        SysUser userInfo = (SysUser) params.get("userInfo");
        Map company = companyMapper.getCompany(params);
        companyMapper.rejectCompany(params);
        Map notify = new HashMap();
        notify.put("sendAccount",userInfo.getUserAccount());
        notify.put("sendType",0);
        notify.put("notifyType",1);
        notify.put("internshipId",params.get("internshipId"));
        notify.put("content","管理员"+userInfo.getUserName()+"已驳回了你上传的实训单位！");
        notify.put("targetAccount",company.get("creatAccount"));
        notify.put("targetType",0);
        notify.put("creatAccount",params.get("teachAccount"));
        notifySV.addNotify(notify);
        return result;
    }

}
